package es.seam.myebay.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;

@Name("account")
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class Account implements Serializable {
    private static final long serialVersionUID = 8444287111124328025L;

    private Integer id;
    private String name;
    private Date memberSince;
    private User user;
    private List<Bid> bids;
    private List<Auction> auctions;
    
    
    @Id
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    public void setId(Integer accountId) {
        this.id = accountId;
    }

    @NotNull
    @Length(min = 3, max = 40)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getMemberSince() {
        return memberSince;
    }

    public void setMemberSince(Date memberSince) {
        this.memberSince = memberSince;
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "id_user")
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }

    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="account")
    public List<Bid> getBids() {
        return bids;
    }
    public void setBids(List<Bid> bids) {
        this.bids = bids;
    }
    
    @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="account")
    public List<Auction> getAuctions() {
        return auctions;
    }

    public void setAuctions(List<Auction> auctions) {
        this.auctions = auctions;
    }

    
    @Override
    public boolean equals(Object value) {
        if (!(value instanceof Account))
            return false;

        Account other = (Account) value;

        return other.id != null
                && other.id.equals(this.id);
    }

    @Override
    public int hashCode() {
        return id != null ? id.intValue() : super.hashCode();
    }
}